// packageA/pages/approve/approveList/approveList.js
const app = getApp();
import CommonService from '../../../../utils/service/commonService.js';
const util = require('../../../../utils/util');
import Base64 from '../../../../utils/Base64';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        user: null,
        authority: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            user: app.globalData.userInfo,
            authority: app.globalData.authority
        })

        this.getMenu();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //获取子菜单
    getMenu() {
        let param = {
            isAuth: false,
            className: 'menu',
            data: {
                searchAll: true,
                appId: 3,
                fid: 382,
                selectFields: ['id', 'title', 'path', 'ordernum', 'icon', 'perms'],
                nbl: Base64.encode(util.timeFormatDay(new Date())),
                sort: {
                    ordernum: 1
                },
            }
        }

        CommonService.goFindList(param).then(res => {
            console.log("res");
            console.log(res);
            if (res.data.data && res.data.data.result && res.data.data.result.length) {
                let result = res.data.data.result;
                result.forEach((item, index) => {
                    //处理权限
                    if (this.checkKey(item.perms)) {
                        item.showMenu = true;
                    } else {
                        item.showMenu = false;
                    }
                });
                console.log('子菜单列表');
                console.log(result);

                this.setData({
                    list: result
                })
            }
        }).catch((error) => {
            console.error(error);
        })
    },

    //点击菜单
    goToUrl({
        currentTarget
    }) {
        let detail = currentTarget.dataset.detail;
        let _this = this;

        if (detail.path == '/pages/menu/menu') {
            //如果还有子菜单
            let obj = {
                id: detail.id,
                title: detail.title
            }
            wx.navigateTo({
                url: "/pages/menu/menu?paramObj=" + JSON.stringify(obj)
            })
        } else {
            wx.navigateTo({
                url: detail.path,
                fail: function () {
                    _this.goToNot();
                }
            })
        }
    },

    //菜单根据权限来显示
    checkKey(authority) {
        console.log(authority);
        let currentRole = this.data.authority;
        console.log("currentRole");
        console.log(currentRole);
        let authArr = authority.split(':');
        let checkResult = true;

        authArr.forEach((auth, index) => {
            if (index == 0) {
                if (currentRole[auth] && currentRole[auth] != undefined) {
                    if (currentRole[auth] == {}) {
                        checkResult = false;
                    } else {
                        checkResult = currentRole[auth];
                    }
                } else {
                    checkResult = false;
                }
            } else {
                if (checkResult[auth] && checkResult[auth] != undefined) {
                    if (checkResult[auth] == {}) {
                        checkResult = false;
                    } else {
                        checkResult = checkResult[auth];
                    }
                } else {
                    checkResult = false;
                }
            }
        });

        return checkResult;
    },

    setBorder(item) {
        console.log("set border...");
    }
})